#include <Arduino.h>
#include "MusicLyrics.h"
#include "audio_hal.h"


extern char music_lyrics[100];

NS_DT_BEGIN

const uint8_t icon_music_Lyrics[] PROGMEM =
{
    0x00,0x3F,0x21,0x21,0x21,0xE7,0xE7,0x00
};

const DTRGB LyricsColor[LYRICS_COLOR_NUM] = {
    {0xEE,0x6A,0x50}, //???
    {0xC8,0x00,0x00}, //??
    {0xF9,0x58,0x00}, //??
    {0xFA,0xC5,0x15}, //??
    {0x4D,0xEB,0x02}, //??
    {0x1A,0xE6,0xE6}, //??
    {0x00,0x2D,0xFF}, //??
    {0x91,0x2B,0xD5}  //??
};

bool MusicLyrics::init()
{
    MusicLyricsLayer *rootLayer = MusicLyricsLayer::create();
    rootLayer->setContentSize(Size(MATRIX_WIDTH,MATRIX_HEIGHT));
    this->addChild(rootLayer);
    rootLayer->initLayer();
    return true;
}
MusicLyricsLayer::~MusicLyricsLayer()
{

}
bool MusicLyricsLayer::initLayer()
{
    Size size = this->getContentSize();
    CanvasSprite *canvasSprite = CanvasSprite::create(size.width,size.height);
    this->addChild(canvasSprite);
    //this->scheduleUpdate();
    canvas = canvasSprite->getSpriteCanvas();

    canvas->drawSmartStr(0,2,"�������",DTRGB(0xff,0,0xff),DTRGB(0,0,0),12);

    this->schedule(DT_SCHEDULE_SELECTOR(MusicLyricsLayer::update),0.025);
    return true;
}
void MusicLyricsLayer::update(float dt)
{
    uint8_t lyrics_font = 0;
    static uint8_t flag_update = 0;
    static uint16_t lyrics_len = 0;
    static int16_t x_pose = 0;
    static char last_lyrics[100] = {0};
    
    if(flag_update==1 && lyrics_len>0){
        canvas->canvasReset();
    }
    if(strncmp(music_lyrics,last_lyrics,strlen(music_lyrics))!=0){
        memcpy(last_lyrics,music_lyrics,strlen(music_lyrics));
        flag_update = 1;
        x_pose = 0;
    }
    lyrics_len = strlen(music_lyrics)*6;
    lyrics_font = app_mode_page_get(MENU_PAGE_LYRICS);
    canvas->drawSmartStr(x_pose,2,music_lyrics,LyricsColor[lyrics_font],DTRGB(0,0,0),12);
    // lyrics_len = strlen("   Hello I'm ???????? Made by xiaomingst  ????????????????????????  ??????~  [PS:??????????????Arduino+ESP32+ES8388  Contact with me: mingle@xiaomingst.tech      ]")*6;
    // lyrics_font = app_mode_page_get(MENU_PAGE_LYRICS);
    // canvas->drawSmartStr(x_pose,2,"   Hello I'm ???????? Made by xiaomingst  ????????????????????????  ??????~  [PS:??????????????Arduino+ESP32+ES8388  Contact with me: mingle@xiaomingst.tech      ]",DTRGB(0xff,0,0xff),DTRGB(0,0,0),12);
       
    x_pose--;

    if((-(x_pose))>=lyrics_len){
        x_pose = 0;
    }
}

NS_DT_END

